package com.example.ffepeg.ffeAdder;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class FfmpegVideo {
    public static void main(String[] args) {
        String inputVideo1 = "C:\\Users\\Administrator\\Desktop\\123415.mp4";
        String inputVideo2 = "C:\\Users\\Administrator\\Desktop\\d4d312da-204e-4ea6-8ee5-8685b0812a0b.mp4";
        String inputVideo3 = "C:\\Users\\Administrator\\Desktop\\ca1a316b-0282-4dba-b74f-127971a9292d.mp4";
        String inputVideo4 = "C:\\Users\\Administrator\\Desktop\\123415.mp4";
        String inputVideo5 = "C:\\Users\\Administrator\\Desktop\\d4d312da-204e-4ea6-8ee5-8685b0812a0b.mp4";
        String inputVideo6 = "C:\\Users\\Administrator\\Desktop\\123415.mp4";
        String inputVideo7 = "C:\\Users\\Administrator\\Desktop\\ca1a316b-0282-4dba-b74f-127971a9292d.mp4";
        String inputVideo8 = "C:\\Users\\Administrator\\Desktop\\123415.mp4";
        String inputVideo9 = "C:\\Users\\Administrator\\Desktop\\d4d312da-204e-4ea6-8ee5-8685b0812a0b.mp4";
        String inputVideo10 = "C:\\Users\\Administrator\\Desktop\\ca1a316b-0282-4dba-b74f-127971a9292d.mp4";
        String outputFile = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\out\\twoSixfiles00k.mp4";

        // 视频文件路径列表
        List<String> videoFiles = List.of(inputVideo1,inputVideo2,inputVideo3,inputVideo4,inputVideo5,inputVideo6,inputVideo7,inputVideo8,inputVideo9,inputVideo10);

        // 视频列表文件路径
        String listFile = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\filelist.txt";

        // 创建视频列表文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(listFile))) {
            for (String videoFile : videoFiles) {
                // 写入文件路径，注意使用绝对路径或确保相对路径正确
                writer.write("file '" + new File(videoFile).getAbsolutePath() + "'\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        // 构建FFmpeg命令
        String ffmpegCmd = "ffmpeg -f concat -safe 0 -i " + listFile + " -c copy " + outputFile;

        // 执行FFmpeg命令
        try {
            Process process = Runtime.getRuntime().exec(ffmpegCmd);
            process.waitFor();
            System.out.println("视频合并完成!");
            FileWriter fw = new FileWriter(listFile);
            fw.write("");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
